package per.dg.sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import per.dg.sqlite.original.MySQLiteOpenHelper;
import per.dg.sqlite.original.SqliteManager;

public class SqliteMainActivity extends AppCompatActivity {


    EditText mEtName, mEtNameDelete, mEtNameOld, mEtNameNew;
    TextView mTvNames;
    Button mBtInsert, mBtQuery, mBtDelete, mBtUpdate;

    SqliteManager mSqliteManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_main);

        mSqliteManager = SqliteManager.getInstance(this);

        mEtName = findViewById(R.id.et_name);
        mTvNames = findViewById(R.id.tv_names);
        mBtInsert = findViewById(R.id.bt_insert);
        mBtInsert.setOnClickListener(mOnClickListener);
        mBtQuery = findViewById(R.id.bt_query);
        mBtQuery.setOnClickListener(mOnClickListener);
        mEtNameDelete = findViewById(R.id.et_name_delete);
        mEtNameOld = findViewById(R.id.et_name_old);
        mEtNameNew = findViewById(R.id.et_name_new);
        mBtDelete = findViewById(R.id.bt_delete);
        mBtDelete.setOnClickListener(mOnClickListener);
        mBtUpdate = findViewById(R.id.bt_update);
        mBtUpdate.setOnClickListener(mOnClickListener);
    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bt_insert:
//                    long insert = mSqliteManager.insert(mEtName.getText().toString());
//                    if(insert != -1){
//                        mEtName.setText("");
//                        Toast.makeText(SqliteMainActivity.this, "插入成功", Toast.LENGTH_SHORT).show();
//                    }else {
//                        Toast.makeText(SqliteMainActivity.this, "插入失败", Toast.LENGTH_SHORT).show();
//                    }

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            mSqliteManager.insert(mEtName.getText().toString());
                        }
                    }).start();
                    break;
                case R.id.bt_query:
//                    mTvNames.setText(mSqliteManager.query());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            mSqliteManager.query();
                        }
                    }).start();
                    break;
                case R.id.bt_delete:
                    if(mSqliteManager.delete(new String[]{mEtNameDelete.getText().toString()}) == 0){
                        Toast.makeText(SqliteMainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(SqliteMainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.bt_update:
                    if(mSqliteManager.update(mEtNameNew.getText().toString(), mEtNameOld.getText().toString()) == 0){
                        Toast.makeText(SqliteMainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(SqliteMainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };
}
